Slide 18

1.

Because of S3’s generic.class() naming scheme, both functions may initially look similar, while they are in fact unrelated.

t.test() is a generic function that performs a t-test. t.data.frame() is a method that gets called by the generic t() to transpose data frame input. Due to R’s S3 dispatch rules, t.test() would also get called when t() is applied to an object of class test

2.

It returns an object of the class ecdf (empirical cumulative distribution function) with the superclasses stepfun and function. The ecdf object is built on the base type closure (a function). The expression, which was used to create it (rpois(100, 10)), is stored in the call attribute.

3.

This code returns a table object, which is built upon the integer type. The attribute dimnames is used to name the elements of the integer vector.

slide 35

1.

We can see that t.test() is a generic because it calls UseMethod():

t.test
## function (x, ...) 
## UseMethod("t.test")
## <bytecode: 0x0000024b6630d678>
## <environment: namespace:stats>
# or simply call
sloop::ftype(t.test)
## [1] "S3"      "generic"

Interestingly, R also provides helpers, which list functions that look like methods, but in fact are not:

tools::nonS3methods("stats")
## [1] "anova.lmlist"        "expand.model.frame"  "fitted.values"      
## [4] "influence.measures"  "lag.plot"            "t.test"             
## [7] "plot.spec.phase"     "plot.spec.coherency"

When we create an object with class test, t() dispatches to the t.default() method. This happens, because UseMethod() simply searches for functions named paste0(“generic”, “.”, c(class(x), “default”)).

x <- structure(1:10, class = "test")

t(x)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]    1    2    3    4    5    6    7    8    9    10
## attr(,"class")
## [1] "test"

However, in older versions of R (pre R 4.0.0; when Advanced R was written) this behaviour was slightly different. Instead of dispatching to the t.default() method, the t.test() generic was erroneously treated as a method of t() which then dispatched to t.test.default() or (when defined) to t.test.test().

2.

A: This is a simple application of sloop::s3_methods_class():

s3_methods_class("table")
## # A tibble: 10 × 4
##    generic       class visible source             
##    <chr>         <chr> <lgl>   <chr>              
##  1 [             table TRUE    base               
##  2 aperm         table TRUE    base               
##  3 as.data.frame table TRUE    base               
##  4 Axis          table FALSE   registered S3method
##  5 lines         table FALSE   registered S3method
##  6 plot          table FALSE   registered S3method
##  7 points        table FALSE   registered S3method
##  8 print         table TRUE    base               
##  9 summary       table TRUE    base               
## 10 tail          table FALSE   registered S3method

Interestingly, the table class has a number of methods designed to help plotting with base graphics.